package cn.cvs.controller;

import cn.cvs.pojo.SysUser;
import cn.cvs.service.SysUserService;
import cn.cvs.utils.BigDecimalUtil;
import cn.cvs.utils.Constants;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    private SysUserService sysUserService;


    // 判断账号密码是否存在
    @RequestMapping("/login")
  public  String findSys(@RequestParam String account,
                         @RequestParam String password,
                         HttpServletRequest request,
                         HttpSession session
                            ) {

        SysUser sysUser = sysUserService.findSys(account,password);

        if ( sysUser != null){
            session.setAttribute(Constants.USER_SESSION,sysUser);
            return "redirect:/frame";
        } else{
            request.setAttribute("error","账号或密码错误，请重新输入");
            return"login";
        }
    }

   /* //登录成功
    @RequestMapping("/frame")
    public String getFrame(){
        return "frame";
    }*/




    // 登录成功
    @RequestMapping("/frame")
    public String getFrame(HttpSession session) {
        if (session.getAttribute(Constants.USER_SESSION) == null) {
            return "redirect:/login"; // 如果用户未登录，重定向到登录页面
        }
        return "frame";
    }

    // 退出系统
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate(); // 使会话失效，从而实现退出
        return "login"; // 重定向到登录页面
    }
}



